﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zero.Core.Domain;
using Zero.POCO.FiltreEnumeration;

namespace Zero.POCO
{
    public class Filter : EntityBase<Guid>,IAggregateRoot
    {
        static List<int> _premier = new List<int>() { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127 };
       
        public string Nom { get;  set; }
         public int ageMin { get;set; }
         public int ageMax { get;  set; }
         public int CSEs { get; set; }
         public int CSPs { get; set; }
         public int Equipements { get; set; }
         public int Genres { get; set; }
         public int Habitats { get; set; }
         public int NbChambres { get; set; }
         public int NbVoitures { get; set; }
         public int NiveauxInstructions { get; set; }
         public int RevenueTotalFoyers { get; set; }
         //public List<Salaire> Salaires { get;  set; }
         public int SituationMatrimonials { get; set; }
         public int StatutFamiliales { get; set; }
         public int TailleFoyers { get; set; }
         public int TypeHabitations { get; set; }
         public int Representativite { get; set; }
        protected override void Validate()
        {
            throw new NotImplementedException();
        }

        public int GetRepresentativite()
        {
            //TODO : implementer la fonctionnalité
            return 50;
        }
        public List<int> GetFiltrePrimaire()
        {

            List<int> ListCse=new List<int>();
            List<int> ListCPs=new List<int>();
            List<int> Listgenre=new List<int>();
            List<int> ListNivIns = new List<int>();
            List<int> ListRevenue = new List<int>();
            int value = 1;

            /***************CSE*************************/
            /*if (CSEs.Any())
            {
                List<int> listeCse = CSEs.Select(Cse => (int) Cse).ToList();
                sigElu = sigElu.Where(e => listeCse.Contains((int)e.CSE)).ToList();
            }*/


            var Class_a = FiltreFactory.DesassembleFiltre(CSEs, _premier[0]);
            if (Class_a)
            {
                ////value = value * _premier[19];
                ListCse.Add(_premier[19]);
            }

            var Class_b = FiltreFactory.DesassembleFiltre(CSEs, _premier[1]);
            if (Class_b)
            {
                ////value = value * _premier[20];
                ListCse.Add(_premier[20]);
            }

            var Class_c = FiltreFactory.DesassembleFiltre(CSEs, _premier[2]);
            if (Class_c)
            {
                //value = value * _premier[21];
                ListCse.Add(_premier[21]);
            }

            var Class_d = FiltreFactory.DesassembleFiltre(CSEs, _premier[3]);
            if (Class_d)
            {
                //value = value * _premier[22];
                ListCse.Add(_premier[22]);
            }


            /***************CSP*************************/
            /*
            if (CSPs.Any())
            {
                var listeCsp = CSPs.Select(Csp => (int) Csp).ToList();
                sigElu = sigElu.Where(e => listeCsp.Contains((int)e.CSP)).ToList();
            }*/


            var HautFonc = FiltreFactory.DesassembleFiltre(CSPs, _premier[0]);
            if (HautFonc)
            {
                ListCPs.Add(_premier[8]);
            }

            var ProfLibe = FiltreFactory.DesassembleFiltre(CSPs, _premier[1]);
            if (ProfLibe)
            {
                ListCPs.Add(_premier[12]);
                //value = value * _premier[12];
            }

            var Cadre = FiltreFactory.DesassembleFiltre(CSPs, _premier[2]);
            if (Cadre)
            {
                ListCPs.Add(_premier[2]);
                //value = value * _premier[2];
            }

            var CadreMoy = FiltreFactory.DesassembleFiltre(CSPs, _premier[3]);
            if (CadreMoy)
            {
                ListCPs.Add(_premier[3]);
                //value = value * _premier[3];
            }


            var Emp = FiltreFactory.DesassembleFiltre(CSPs, _premier[4]);
            if (Emp)
            {
                ListCPs.Add(_premier[5]);
                //value = value * _premier[5];
            }



            var PetitPatro = FiltreFactory.DesassembleFiltre(CSPs, _premier[5]);
            if (PetitPatro)
            {
                ListCPs.Add(_premier[11]);
                //value = value * _premier[11];
            }

            var Chom = FiltreFactory.DesassembleFiltre(CSPs, _premier[6]);
            if (Chom)
            {
                ListCPs.Add(_premier[4]);
                //value = value * _premier[4];
            }




            var Fof = FiltreFactory.DesassembleFiltre(CSPs, _premier[7]);
            if (Fof)
            {
                //value = value * _premier[7];
                ListCPs.Add(_premier[7]);
            }


            var Etud = FiltreFactory.DesassembleFiltre(CSPs, _premier[8]);
            if (Etud)
            {
                ListCPs.Add(_premier[6]);
                //value = value * _premier[6];
            }

            var Retret = FiltreFactory.DesassembleFiltre(CSPs, _premier[9]);
            if (Retret)
            {
                ListCPs.Add(_premier[13]);
                //value = value * _premier[13];
            }

            var Passion = FiltreFactory.DesassembleFiltre(CSPs, _premier[10]);
            if (Passion)
            {
                ListCPs.Add(_premier[10]);
                //value = value * _premier[10];
            }

            var Autre = FiltreFactory.DesassembleFiltre(CSPs, _premier[11]);
            if (Autre)
            {
                ListCPs.Add(_premier[9]);
                //value = value * _premier[9];
            }

            /***************Situation Familiale*************************/




            /*if (StatutFamiliales.Any())
            {
                var listeSitFam = StatutFamiliales.Select(SitFam => (int) SitFam).ToList();
                sigElu = sigElu.Where(e => listeSitFam.Contains((int)e.Statutfamilial)).ToList();

            }*/








            /***************Genre*************************/
            /*if (Genres.Any())
            {
                var listeGenre = Genres.Select(genre => (int) genre).ToList();
                sigElu = sigElu.Where(e => listeGenre.Contains((int)e.Genre)).ToList();
            }*/

            var Homme = FiltreFactory.DesassembleFiltre(Genres, _premier[0]);
            if (Homme)
            {
                Listgenre.Add(_premier[0]);
                //value = value * _premier[0];
            }

            var Femme = FiltreFactory.DesassembleFiltre(Genres, _premier[1]);
            if (Femme)
            {
                Listgenre.Add(_premier[1]);
                //value = value * _premier[1];
            }




            /***************NiveauxInstructions*************************/
            /*if (NiveauxInstructions.Any())
            {
                var niveauxInstructions = NiveauxInstructions.Select(instance => (int) instance).ToList();
                sigElu = sigElu.Where(e => niveauxInstructions.Contains((int)e.Niveau)).ToList();
            }*/
            var Sans = FiltreFactory.DesassembleFiltre(NiveauxInstructions, _premier[0]);
            if (Sans)
            {
                ListNivIns.Add(_premier[27]);
                //value = value * _premier[27];
            }

            var Analph = FiltreFactory.DesassembleFiltre(NiveauxInstructions, _premier[1]);
            if (Analph)
            {
                ListNivIns.Add(_premier[26]);
                //value = value * _premier[26];
            }

            var prim = FiltreFactory.DesassembleFiltre(NiveauxInstructions, _premier[2]);
            if (prim)
            {
                //value = value * _premier[24];
                ListNivIns.Add(_premier[24]);
            }

            var Moyen = FiltreFactory.DesassembleFiltre(NiveauxInstructions, _premier[3]);
            if (Moyen)
            {
                //value = value * _premier[23];
                ListNivIns.Add(_premier[23]);
            }


            var Second = FiltreFactory.DesassembleFiltre(NiveauxInstructions, _premier[4]);
            if (Second)
            {
                //value = value*_premier[25];
                ListNivIns.Add(_premier[25]);
            }
            var univ = FiltreFactory.DesassembleFiltre(NiveauxInstructions, _premier[5]);
            if (univ)
            {
                ListNivIns.Add(_premier[28]);
                //value = value * _premier[28];
            }

            /***************RevenueTotalFoyers*************************/
            /*if (NiveauxInstructions.Any())
            {
                var RevenueTotalFoyers = RevenueTotalFoyers.Select(instance => (int) instance).ToList();
                sigElu = sigElu.Where(e => RevenueTotalFoyers.Contains((int)e.RevenueTotalFoyer)).ToList();
            }*/

            var Mo12 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[0]);
            if (Mo12)
            {
                ListRevenue.Add(_premier[18]);
                //value = value * _premier[18];
            }


            var Entre12 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[1]);
            if (Entre12)
            {
                ListRevenue.Add(_premier[14]);
                //value = value * _premier[14];
            }

            var Entre20 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[2]);
            if (Entre20)
            {
                ListRevenue.Add(_premier[15]);
                //value = value * _premier[15];
            }

            var Entre35 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[3]);
            if (Entre35)
            {
                ListRevenue.Add(_premier[14]);
                //value = value * _premier[14];
            }

            var Entre50 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[4]);
            if (Entre50)
            {
                ListRevenue.Add(_premier[29]);
                //value = value * _premier[29];
            }

            var Entre90 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[5]);
            if (Entre90)
            {
                ListRevenue.Add(_premier[16]);
                //value = value * _premier[16];
            }

            var Entre150 = FiltreFactory.DesassembleFiltre(RevenueTotalFoyers, _premier[6]);
            if (Entre150)
            {
                ListRevenue.Add(_premier[17]);
                //value = value * _premier[17];
            }






            if(ListCse.Count==0) ListCse.Add(1);
            if(ListCPs.Count == 0)ListCPs.Add(1);
            if(Listgenre.Count == 0)Listgenre.Add(1);
            if(ListNivIns.Count == 0)ListNivIns.Add(1);
            if (ListRevenue.Count == 0)ListRevenue.Add(1) ;
            var results = new List<int>();

            foreach (var rev in ListRevenue)
            {
                foreach (var niv in ListNivIns)
                {
                    foreach (var genre in Listgenre)
                    {
                        foreach (var cse in ListCse)
                        {
                            foreach (var csp in ListCPs)
                            {
                                var res = rev*niv*genre*cse*csp;
                                if(res!=1)
                                results.Add(res);
                            }
                        }
                    }
                }
            }

            return results;


        }

        public int GetFiltreSecondaire()
        {
            int value = 1;


            /***************Situation Familiale*************************/






            var Chef = FiltreFactory.DesassembleFiltre(StatutFamiliales, _premier[0]);
            if (Chef)
                value = value * _premier[1];

            var maitr = FiltreFactory.DesassembleFiltre(StatutFamiliales, _premier[1]);
            if (maitr)
                value = value * _premier[3];

            var enfant = FiltreFactory.DesassembleFiltre(StatutFamiliales, _premier[2]);
            if (enfant)
                value = value * _premier[2];

            var autrStat = FiltreFactory.DesassembleFiltre(StatutFamiliales, _premier[3]);
            if (autrStat)
                value = value * _premier[0];



            /***************Habitats*************************/
            /*if (Habitats.Any())
            {
                var listeHabitats = Habitats.Select(habitat => (int) habitat).ToList();
                sigElu = sigElu.Where(e => listeHabitats.Contains((int)e.Habitat)).ToList();
            }*/

            var prop = FiltreFactory.DesassembleFiltre(Habitats, _premier[0]);
            if (prop)
                value = value * _premier[14];

            var loc = FiltreFactory.DesassembleFiltre(Habitats, _premier[1]);
            if (loc)
                value = value * _premier[13];

            /***************NbChambres*************************/

            /*
            if (NbChambres.Any())
            {
                var nbChambres = NbChambres.Select(NbChambre => (int) NbChambre).ToList();
                sigElu = sigElu.Where(e => nbChambres.Contains((int)e.NbPiece)).ToList();
            }*/


            var Chambr1 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[0]);
            if (Chambr1)
                value = value * _premier[8];

            var Chambr2 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[1]);
            if (Chambr2)
                value = value * _premier[5]; ;

            var Chambr3 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[2]);
            if (Chambr3)
                value = value * _premier[7];

            var Chambr4 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[3]);
            if (Chambr4)
                value = value * _premier[6];

            var Chambr5 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[4]);
            if (Chambr5)
                value = value * _premier[4];


            /***************NbVoitures*************************/

            /*if (NbVoitures.Any())
            {
                var NbVoitures = NbVoitures.Select(instance => (int) instance).ToList();
                sigElu = sigElu.Where(e => NbVoitures.Contains((int)e.NbVoiture)).ToList();
            }*/

            var Voiture0 = FiltreFactory.DesassembleFiltre(NbVoitures, _premier[0]);
            if (Voiture0)
                value = value * _premier[9];

            var Voiture1 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[0]);
            if (Voiture1)
                value = value * _premier[10]; ;

            var Voiture2 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[0]);
            if (Voiture2)
                value = value * _premier[11];

            var Voiture3 = FiltreFactory.DesassembleFiltre(NbChambres, _premier[0]);
            if (Voiture3)
                value = value * _premier[12];





            /***************SituationMatrimonials*************************/
            /*if (SituationMatrimonials.Any())
            {
                var listeSituationMatrimonials = SituationMatrimonials.Select(habitat => (int)habitat).ToList();
                sigElu = sigElu.Where(e => listeSituationMatrimonials.Contains((int)e.Situationmatrimoniale)).ToList();
            }
            */


            /***************TailleFoyers********z*****************/
            /*if (TailleFoyers.Any())
            {
                var listeTailleFoyers = TailleFoyers.Select(habitat => (int) habitat).ToList();
                sigElu = sigElu.Where(e => listeTailleFoyers.Contains((int)e.TailleFoyer)).ToList();
            }*/



            /***************TypeHabitat********z*****************/
            /*if (TypeHabitations.Any())
            {
                var listeTypeHabitations = TypeHabitations.Select(instance => (int) instance).ToList();
                sigElu = sigElu.Where(e => listeTypeHabitations.Contains((int)e.TypeHabitat)).ToList();
            }*/

            var toile = FiltreFactory.DesassembleFiltre(TypeHabitations, _premier[0]);
            if (toile)
                value = value * _premier[19];

            var tradit = FiltreFactory.DesassembleFiltre(TypeHabitations, _premier[1]);
            if (tradit)
                value = value * _premier[20];

            var appart = FiltreFactory.DesassembleFiltre(TypeHabitations, _premier[2]);
            if (appart)
                value = value * _premier[16]; ;

            var appartsta = FiltreFactory.DesassembleFiltre(TypeHabitations, _premier[3]);
            if (appartsta)
                value = value * _premier[17];


            var villajum = FiltreFactory.DesassembleFiltre(TypeHabitations, _premier[5]);
            if (villajum)
                value = value * _premier[22];

            var villaindep = FiltreFactory.DesassembleFiltre(TypeHabitations, _premier[6]);
            if (!villaindep)
                value = value * _premier[21];


            return value;


        }
       
    }
}
